//
//  ZPMineClassViewController.m
//  ZPIOSEducation
//
//  Created by 李伟 on 2016/12/4.
//  Copyright © 2016年 mini1. All rights reserved.
//

#import "ZPMineClassViewController.h"
#import "ZPMyClassData.h"
#import "ZPEmptyGradeView.h"

#import "ZPStateLearningTabCell.h"
#import "ZPStateReadyTabCell.h"
#import "ZPStateCompleteTabCell.h"

#import "ZPStateAllViewController.h"
#import "ZPStateLearningViewController.h"
#import "ZPStateReadyViewController.h"
#import "ZPStateCompleteViewController.h"
#import "ZPMineClassChangeRecordVC.h"

#define studySateScrollV_H 44.
#define myClassTableViewSectionCount 3

static NSString * const reuseIdentifier_learning = @"ZPStateLearningTabCell";
static NSString * const reuseIdentifier_ready    = @"ZPStateReadyTabCell";
static NSString * const reuseIdentifier_complete = @"ZPStateCompleteTabCell";

@interface ZPMineClassViewController () <UIScrollViewDelegate>
{
   // NSMutableArray   * classSateItems; // 按钮数组
   // CGFloat            itemsWidth; // 滚动视图滚动的内容宽度(动态变化)
   // NSInteger          atIndex;    // 标题数组下标
    
    MBProgressHUD *HUD;
    ZPEmptyGradeView *emptyFooterView;
    
}

@property (weak, nonatomic) IBOutlet UITableView *myClassesTableView;

// 课程状态滚动图
@property (strong, nonatomic) UIScrollView       *studySateScrollV;

// 标题下划线
@property (nonatomic,  weak)  UIView             *lineView;

// 主题内容滚动图
@property (strong, nonatomic) UIScrollView       *studyContensScrollV;

// 选中的按钮
@property (nonatomic, weak) UIButton             *selectButton;

// 装载按钮的数组
@property (nonatomic, strong) NSMutableArray     *titleButtons;
@end


@implementation ZPMineClassViewController

- (NSMutableArray *)titleButtons
{
    if (_titleButtons == nil)
    {
        _titleButtons = [NSMutableArray array];
    }
    return _titleButtons;
}


// 学习状态滚动图
- (UIScrollView *)studySateScrollV
{
    if (_studySateScrollV == nil)
    {
        _studySateScrollV = [[UIScrollView alloc] init];
        _studySateScrollV.backgroundColor = UIColorFromRGB(0xF9F9F9);
        _studySateScrollV.frame = CGRectMake(0, 0, Main_Screen_Width, studySateScrollV_H);
        _studySateScrollV.scrollsToTop = NO;
        NSInteger VCcount = self.childViewControllers.count;
        CGFloat btnW =  (Main_Screen_Width - 3) / 4;
        CGFloat btnH =  _studySateScrollV.xj_height;
        for (int i = 0; i < VCcount; i++)
        {
            UIButton *classStateBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            classStateBtn.tag = i;
            // 按钮的标题是自控制器的标题
            UIViewController * VC = self.childViewControllers[i];
            [[ZPShareView sharedSingleView] setupButton:classStateBtn backgroundColor:clear_color image:nil title:VC.title titltColor:UIColorFromRGB(0X999999) titleFont:14.];
            // 设置按钮的位置
            classStateBtn.frame = CGRectMake(i * (btnW + 1), 0, btnW, btnH);
            // 点击
            [classStateBtn addTarget:self action:@selector(clickTitleBotton:) forControlEvents:UIControlEventTouchUpInside];
            // 加载状态按钮
            [_studySateScrollV addSubview:classStateBtn];
            
            // 添加状态按钮之间的竖线
            if (i > 0 ) {
                UIView  *SeparateView = [[UIView alloc] init];
                SeparateView.backgroundColor = UIColorFromRGB(0xEEEEEE);
                SeparateView.frame = CGRectMake(i * (btnW + 1), (btnH-14)/2, 1, 14);
                [_studySateScrollV addSubview:SeparateView];
            }
            
            // 添加下划线
            // 下划线的宽度    = 标题文字的宽度
            // 下划线的中心点x = 标题按钮中心点x
            if (i == 0)
            {
              
                CGFloat h = 1.5;
                CGFloat y = 40.0;
                UIView * lineView = [[UIView alloc] init];
                // 位置尺寸
                lineView.xj_height = h;
                // 先计算标题按钮的尺寸,再给下滑线标签赋值
                [classStateBtn.titleLabel sizeToFit];
                // 宽
                lineView.xj_width = classStateBtn.xj_width / 2;
                // 中心点x
                lineView.xj_centerX = classStateBtn.xj_centerX;
                // y点坐标
                lineView.xj_y = y;
                // 下划线背景色
                lineView.backgroundColor = zMainColor;
                // 引用
                _lineView = lineView;
                // 循环加载下划线
                [_studySateScrollV addSubview:lineView];
                // 点击标题按钮方法
                [self clickTitleBotton:classStateBtn];
            }
            // 标题按钮循环添加到数组中
            [self.titleButtons addObject:classStateBtn];
        }
    }
    return _studySateScrollV;
}


// 主体滚动图
- (UIScrollView *)studyContensScrollV
{
    if (_studyContensScrollV == nil) {
        _studyContensScrollV = [[UIScrollView alloc] init];
        _studyContensScrollV.frame = CGRectMake(0,studySateScrollV_H, Main_Screen_Width, Main_Screen_Height - studySateScrollV_H - 64);
        _studyContensScrollV.contentSize = CGSizeMake(self.childViewControllers.count * Main_Screen_Width, 0);
        _studyContensScrollV.bounces = YES;
        _studyContensScrollV.pagingEnabled = YES;
        _studyContensScrollV.showsHorizontalScrollIndicator = NO;
        _studyContensScrollV.delegate = self;
        for (int i = 0; i < self.childViewControllers.count; i++)
        {
            UIView * childView = self.childViewControllers[i].view;
            childView.tag = i;
            childView.frame = CGRectMake(Main_Screen_Width * i, 0, Main_Screen_Width, _studyContensScrollV.xj_height);
//            childView.backgroundColor = zRandomColor;
            [_studyContensScrollV addSubview:childView];
        }
    }
    return _studyContensScrollV;
}



#pragma mark - <viewDidLoad>
- (void)viewDidLoad {
    [super viewDidLoad];
    // self.automaticallyAdjustsScrollViewInsets = NO;  // 滚动的布局不要由系统控制
    [self setupAllChildViewController]; // 先加载子控制器 再加载课程状态滚动图
    [self setupClassSateScrollV];       // 班级状态top
    [self setupClassContensScrollV];    // 班级详细
//    [self showNoDataViewWithKeyWord:nil]; // 没有相关班级
}



- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.title = @"我的班级";
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"zhenpin_back"] style:UIBarButtonItemStylePlain target:self action:@selector(backButtonClick)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"变更记录" style:UIBarButtonItemStylePlain target:self action:@selector(enterChangeRecord)];
    [self.navigationItem.rightBarButtonItem setTitleTextAttributes:@{NSFontAttributeName:Z15,NSForegroundColorAttributeName:white_color} forState:UIControlStateNormal];

}
- (void)backButtonClick
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)enterChangeRecord
{
    ZPMineClassChangeRecordVC *changeRecordVC = [[ZPMineClassChangeRecordVC alloc] init];
    [self.navigationController pushViewController:changeRecordVC animated:YES];
}



- (void)setupClassSateScrollV
{
    [self.view addSubview:self.studySateScrollV];
}
- (void)setupClassContensScrollV
{
    [self.view addSubview:self.studyContensScrollV];
}

#pragma mark - <添加所有的子控制器>
- (void)setupAllChildViewController
{
    // 全部的课程
    ZPStateAllViewController * stateAllVc = [[ZPStateAllViewController alloc] init];
    stateAllVc.title = @"全部";
    [self addChildViewController:stateAllVc];
    
    // 正在学习中课程
    ZPStateLearningViewController * stateLearningVc = [[ZPStateLearningViewController alloc] init];
    stateLearningVc.title = @"学习中";
    [self addChildViewController:stateLearningVc];
    
    // 准备开课的课程
    ZPStateReadyViewController * stateReadyVc = [[ZPStateReadyViewController alloc] init];
    stateReadyVc.title = @"待开课";
    [self addChildViewController:stateReadyVc];
    
    // 已学完的课程
    ZPStateCompleteViewController * stateCompleteVc = [[ZPStateCompleteViewController alloc] init];
    stateCompleteVc.title = @"已学完";
    [self addChildViewController:stateCompleteVc];
}




#pragma mark - <点击标题按钮>
- (void)clickTitleBotton:(UIButton *)sender {
    
    NSInteger i = sender.tag;
    // 重复点击按钮的时候刷新当前界面
    if (sender == _selectButton) {
        
        ZPLog(@"刷新界面");
    }
    [self selectedClassStateBtnClick:sender];
    
    // collectionView修改偏移量
    [self.studyContensScrollV setContentOffset:CGPointMake(Main_Screen_Width * i, 0) animated:YES];
}



- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
    // 获取角标
    NSInteger i = scrollView.contentOffset.x / Main_Screen_Width;
    
    // 获取选中按钮
    UIButton *selectButton = self.titleButtons[i];
    
    // 调用选中标题方法,实现下划线跟随crollView偏移而位移
    [self selectedClassStateBtnClick:selectButton];
}



// 被点击的按钮
- (void)selectedClassStateBtnClick:(UIButton *)stateBtn
{
    _selectButton.selected  = NO;
    stateBtn.selected  =  YES;
    _selectButton = stateBtn;
    [stateBtn setTitleColor:zMainColor forState:UIControlStateNormal];
//    [_selectButton setTitleColor:UIColorFromRGB(0x999999) forState:UIControlStateNormal];
    // 移动下划线的位置
    [UIView animateWithDuration:0.2 animations:^{
        //转移中心点实现位移
        _lineView.xj_centerX = stateBtn.xj_centerX;
    }];
    
    // 未被点击的按钮文字置灰
    for (UIButton *btn in self.titleButtons) {
        if (btn.xj_centerX != _lineView.xj_centerX) {
            [btn setTitleColor:UIColorFromRGB(0x999999) forState:UIControlStateNormal];
        };
    }
}




#pragma mark - <展示无相关班级view>
-(void)showNoDataViewWithKeyWord:(NSString *)key {
    
    if (emptyFooterView) {
        emptyFooterView.hidden = NO;
        [self.view bringSubviewToFront:emptyFooterView];
    } else {
        emptyFooterView = [[[NSBundle mainBundle] loadNibNamed:@"ZPEmptyGradeView" owner:self options:nil] lastObject];
        emptyFooterView.frame = CGRectMake(0,studySateScrollV_H, Main_Screen_Width, Main_Screen_Height - studySateScrollV_H);
        [self.view addSubview:emptyFooterView];
    }
}

-(void)hideDataView {
    if (emptyFooterView) {
        emptyFooterView.hidden = YES;
    }
}



#pragma mark - <网络相关>
#pragma mark - <创建HUD>






- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
